<div class="portlet light bordered">
	<div class="portlet-title">
        <div class="caption font-dark">
            <i class="fa fa-cog font-dark"></i>
            <span class="caption-subject bold">议价单管理 > 议价单列表 > 编辑</span>
        </div>
    </div>
    <div class="portlet-body form">
    <form role="form" action="/quotation/edit" class="form-horizontal" method="POST">
    	<input type="hidden" id="qtId" name ="id" value = "$!item.id">
    	<div class="portlet-title">
             <div class="caption font-dark">
                 <span class="caption-subject bold">基本信息</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" class="btn green buttons-print">打印报价单</a>
             </div>
         </div>
         <div class="form-body left">
           	<div class="form-group">
				<div class="alert alert-warning">
					<strong> 注：</strong>1、只有报价可以修改，修改即保存。2、报价低于售价红色显示、报价高于售价绿色显示、报价等于售价颜色不变。3、差额红色表示。
				</div>
			</div>
         	<div class="form-group">
                <label class="control-label col-md-2">议价单号：</label>
                <div class="control-label col-md-2" style="text-align:left">$!item.id</div>
                <label class="control-label col-md-2">售价总金额：</label>
                <div class="control-label col-md-2" style="text-align:left">$!item.quotationSkuPrice</div>
                <label class="control-label col-md-2">下单时间：</label>
                <div class="control-label col-md-2" style="text-align:left">$!item.timeString</div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-2">申请会员：</label>
                <div class="control-label col-md-2" style="text-align:left">$!userItem.id</div>
                <label class="control-label col-md-2">会员姓名：</label>
                <div class="control-label col-md-2" style="text-align:left">$!userItem.realname</div>
                <label class="control-label col-md-2">所属企业：</label>
                <div class="control-label col-md-2" style="text-align:left">$!companyItem.name</div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-2">会员来源：</label>
                <div class="control-label col-md-2" style="text-align:left">$!userItem.MemberSource</div>
                <label class="control-label col-md-2">议价单有效期：</label>
                
                <div class="col-md-2">
	                <input class="form-control common-time-datepicker-qn" type="text" id="ValidDate" name="ValidDate" value="$!item.ValidDateString" required readonly>
	            </div>
                
                <label class="control-label col-md-2">修改货品数：</label>
                <div class="control-label col-md-2" style="text-align:left" id="modifyNum">$!item.modifyNum</div>
            </div>
            #if($item.quotationState=='DENY_QUOTATION')
            <div class="form-group">
                <label class="control-label col-md-2"><strong>未通过原因：</strong></label>
                <div class="control-label col-md-10" style="text-align:left">$!item.denyDetail</div>
            </div>
            #end
         </div>
         
         <div class="portlet-title row">
             <div class="caption font-dark col-md-1">
                 <span class="caption-subject bold">议价单货品</span>
             </div>
             <p class="col-md-4">
              #if($item.quotationState=='WATI_QUOTATION')
      	 	    <span class="btn default btn-file">
				    <span class="fileinput-new ">导入议价单 </span>
				    <input type="file" class="file" name="" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
			    </span>
         	  #end
         	     <a href="javascript:;" id="exportExcel" class="btn green "><i class="fa fa-download"></i>导出议价单</a>
         	  </p>
         	 
         </div>
         <div class="form-body left">
             <div class="form-group">
                 <div class="col-md-12">
                     <table class="table table-striped table-bordered table-hover">
                         <thead>
                         <tr>
                             <th width="3%">序号</th>
                             <th width="5%">货品编号</th>
                             <th width="12%">货品名称+规格</th>
                             <th width="5%">年采购量</th>
                             <th width="5%">售价</th>
                             <th width="3%">议价</th>
                             
                             <th width="3%">序号</th>
                             <th width="5%">货品编号</th>
                             <th width="12%">货品名称+规格</th>
                             <th width="5%">年采购量</th>
                             <th width="5%">售价</th>
                             <th width="3%">议价</th>
                             
                             <th width="3%">序号</th>
                             <th width="5%">货品编号</th>
                             <th width="12%">货品名称+规格</th>
                             <th width="5%">年采购量</th>
                             <th width="5%">售价</th>
                             <th width="3%">议价</th>
                         </tr>
                         </thead>
                         <tbody id="itemGroup">
                         <tr>
                         #set($countQuantity = 0)
                         #foreach ($!item1 in $!pageQuotationItem.result)
                         #set($sku = $skuMap.get($item1))
                         #set($product = $productMap.get($item1))
                         	 #if($velocityCount > 1)
                         	 	#set($subCount = $velocityCount - 1)
                             	#set($modulo=$subCount % 3)
                             	#if($modulo == 0)
                             		</tr> <tr>
                             	#end
                         	 #end
                             <td>$!velocityCount</td>
                             <td>$!sku.code</td>
                             <td>$!product.name $!sku.attrAndAttrValues</td>
                             <td>$!item1.quantity</td>
                               #set($countQuantity  = ($countQuantity+$!item1.quantity))
                             <td class="skuPrice">$!item1.quotationSkuPrice</td>
                             <td>
                                 <input type="number" class="form-control input-small quotationPrice" step="0.01" skuPrice="$!sku.skuPrice"
                                 	id="quotationPrice_$!item1.id" name="quotationPrice_$!item1.id" value="$!item1.quotationPrice" min="0.01" 
                                 	onchange="javascript:changePrice(this,$!item1.id ,$!item1.quotationPrice)" required>
                             </td>
                          #end
                          </tr>
                          <tr>
                          	<td colspan="15">
                          		市场售价总计：<span class="tQuotationSkuPrice">$!item.quotationSkuPrice</span>元  &nbsp;&nbsp; 
                          		议价单价总计：<span class="tQuotationPrice">$!item.quotationPrice</span>元 &nbsp;&nbsp; 
                          		差额总计：<span class="tDifference">$!tDifference</span>元 &nbsp;&nbsp; 
                          		年采购量总量：<span>$!countQuantity</span>
                          	</td>
                         </tr>
                         </tbody>
                     </table>
                 </div>
                 #set($page= $pageQuotationItem)
				 #set($pageUrl="/quotationItem/edit?$!queryString&pageNo=")
				 #parse("_page.htm")
             </div>
         </div>
         <div class="form-actions right">
				 <input type="hidden" name="quotationState" id="chkQuotationState" value="$item.quotationState" />
				#if($!item.quotationState=='WATI_QUOTATION' || $!item.quotationState== 'BE_ON_QUOTATION')
					<a href="javascript:;" thref="/quotation/list" class="btn green ajax-post"><i class="fa fa-check"></i>提交</a>
				#elseif($item.quotationState=='DENY_QUOTATION')
					<a href="javascript:;" thref="/quotation/list" class="btn green ajax-post"><i class="fa fa-check"></i>继续提交</a>
				#end
				<a href="/quotation/list" class="btn btn-outline grey-salsa ajaxify">返回</a>
         </div>
     </form>   
    <form id="excel_download" action="/quotationItem/exportExcel" style="display: none;"  method="POST">
		<input type="hidden" name="qId" id="qId"  >
		<button type="submit"></button>
	</form> 
    </div>
</div>
<script type="text/javascript">
/**
 * 导入报价单
 */
$(".file").change(function(){
	 var target=$(this);
	 var file = target.context.files[0];
	 var formdata = new FormData();  
	 var quotationId = $("input[name = 'id']").val();
	 
	 formdata.append('file',file);
	 formdata.append('qId',quotationId);
	 
	 	$.ajax({
		url : '/quotationItem/uploadExcel',
		type : 'POST',
		data : formdata,
		async : false,
		cache : false,
		contentType : false,
		processData : false,
		dataType :"JSON",
		success : function(data) {
			if(data.code == 0){
				 toastr.success(data.msg);
			}else{
				 $.each(data,function(i,v){
					 toastr.warning(v);
				 })
			}
		    App.stopPageLoading();
            Layout.fixContentHeight();
            
            $.ajax({
                type: "GET",
                cache: false,
                url: "/quotationItem/edit?id="+quotationId,
                dataType: "html",
                success: function (res) {
                    $('.page-content .page-content-body').html(res);
                }
            });
		} 
	});
})
/**
 * 导出报价单
 */
$("#exportExcel").click(function(){
	var quotationId = $("input[name = 'id']").val();
	$("#qId").val(quotationId);
	$("#excel_download").submit();
})
</script>
<script src="/assets/scripts/views-date.js" type="text/javascript"></script>
<script type="text/javascript">
	function changePrice(obj,id,price){
		var quotationPrice = parseFloat(obj.value);
		if($('#chkQuotationState').val() == 'PASS_QUOTATION') {
			var oldprice = parseFloat(price).toFixed(2);
			toastr.warning('审核通过不可再修改！');
			obj.value = oldprice ;
			return ;
		}
		var formData = new FormData();
        formData.append("id", id);
        formData.append("quotationPrice", quotationPrice);
        
		$.ajax({
			url: "/quotationItem/edit",
			type: "POST",
			data: formData,
			contentType: false,
			processData: false,
			dataType: "json",
			success: function(res) {
				var num = res.data.num;
				$("#modifyNum").text(num);
				var skuPrice = parseFloat(obj.getAttribute('skuPrice'));
				if(quotationPrice < skuPrice){
					obj.style.setProperty('color', 'red');
				}else if(quotationPrice > skuPrice){
					obj.style.setProperty('color', 'green');
				}else{
					obj.style.setProperty('color', '');
				}
				
				var tQuotationPrice = res.data.tQuotationPrice;
				$(".tQuotationPrice").html(tQuotationPrice);
				var tDifference = res.data.tDifference;
				$(".tDifference").html(tDifference);
				
				var tQuotationSkuPrice = parseFloat($(".tQuotationSkuPrice").html());
				tQuotationPrice = parseFloat(tQuotationPrice);
				if(tQuotationPrice < tQuotationSkuPrice){
					$(".tDifference").css("color","red");
				}else if(tQuotationPrice > tQuotationSkuPrice){
					$(".tDifference").css("color","green");
				}else{
					$(".tDifference").css("color","");
				}
			}
		})
	}
	
	function init(){
		var productItem=$('#itemGroup tr');
		var skuPrice = 0;
		var quotationPrice = 0;
		
		for(var j=0; j<productItem.length; j++){
			for(var i=0;i<3;i++){
				skuPrice = parseFloat(productItem.eq(j).find('td.skuPrice').eq(i).html());
				quotationPrice = parseFloat(productItem.eq(j).find('input.quotationPrice').eq(i).val());
				if(quotationPrice < skuPrice){
					productItem.eq(j).find('input.quotationPrice').eq(i).css('color', 'red');
				}else if(quotationPrice > skuPrice){
					productItem.eq(j).find('input.quotationPrice').eq(i).css('color', 'green');
				}
			}
		}
		
		var tQuotationSkuPrice = parseFloat($(".tQuotationSkuPrice").html());
		var tQuotationPrice = parseFloat($(".tQuotationPrice").html());
		if(tQuotationPrice < tQuotationSkuPrice){
			$(".tDifference").css("color","red");
		}else if(tQuotationPrice > tQuotationSkuPrice){
			$(".tDifference").css("color","green");
		}else{
			$(".tDifference").css("color","");
		}
	}
	
	init();
</script>

<script type="text/javascript">
 $(".buttons-print").click(function(){
	 var e = window.open("", "_blank");
     e.location.href="/quotationItem/quotationPrint?qId="+$("#qtId").val();
 
 });
 
 $(".common-time-datepicker-qn").datetimepicker({
     language:  "zh-CN",
     isRTL: App.isRTL(),
     autoclose: true,
     format: "yyyy-mm-dd hh:ii:ss",
     pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left"),
     clearBtn:true,
     todayBtn:false,
     minuteStep:1
 })
</script>
